//
//  InspectionDetailModel.swift
//  Employee
//
//  Created by swordjoy on 2017/4/5.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import Foundation
import SwiftyJSON

struct InspectionDetailModel {
  
    let areaId: String
    let number: String
    let areaName: String
    let region: String
    let itemName: String
    let status: String
    let frequency: String
    let planDate: String
    let day: String
    let actualDate: String
    var result: [InspectionProject]
    let cooperateStaff: String
    let staffName: String
    
    var isCooperate: Bool = false
    var inspectionTime = ""
    
    var isInspected: Bool {
        if status == "Inspected" {
            return true
        }
            return false
    }

    init(json: JSON) {
        self.areaId = json["areaId"].stringValue
        self.number = json["number"].stringValue
        self.areaName = json["areaName"].stringValue
        self.region = json["region"].stringValue
        self.itemName = json["itemName"].stringValue
        self.status = json["status"].stringValue
        self.frequency = json["frequency"].stringValue
        self.planDate = json["planDate"].stringValue
        self.day = json["day"].stringValue
        self.actualDate = json["actualDate"].stringValue

        self.cooperateStaff = json["cooperateStaff"].stringValue
        self.staffName = json["staffName"].stringValue

        let jsonString = json["result"].stringValue

        let data = jsonString.data(using: .utf8)
        if let temp = try? JSONSerialization.jsonObject(with: data!, options: .mutableContainers) {
            let json = JSON.init(temp)
            self.result = InspectionProject.array(from: json.arrayValue)
        }
        else { self.result = [] }
  }
}

struct InspectionProject: ModelProtocol {
  let name: String
  let value: [String]
  let style: String
  
  init(json: JSON) {
    self.name = json["name"].stringValue
    var temp = json["value"].stringValue.components(separatedBy: ",")
    if json["type"].string == "Image" {
        temp.removeAll()
        for d in json["value"].arrayValue {
            temp.append(d["accessUrl"].stringValue)
        }
    }
    self.value = temp
    self.style = json["style"].stringValue
  }
  
  init(name: String) {
    self.name = name
    self.value = ["未填写"]
    self.style = ""
  }
  
  static func array(from jsonArray: [JSON]) -> [InspectionProject] {
    return jsonArray.map({
      return InspectionProject(json: $0)
    })
  }
  
}
